/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.cm;

import java.io.IOException;
import java.sql.SQLException;

import cc.aileron.commons.accessor.ConvertorNotFoundException;
import cc.aileron.commons.exception.ModuleConfigurationException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.commons.utility.ResourceUtils;
import cc.aileron.dao.db.DbConnectionManagerSimple;
import cc.aileron.dao.db.DbDaoModule;
import cc.aileron.template.module.XmlTemplateModule;
import cc.aileron.web.cm.auth.AuthModule;
import cc.aileron.web.cm.dao.DaoConduct;
import static cc.aileron.commons.global.GlobalSetting.*;


import com.google.inject.Binder;
import com.google.inject.Module;

/**
 * @author Aileron
 *
 */
public class ApplicationModule implements Module
{
    /**
     * GLOBAL_SETTING
     */
    private static final String GLOBAL_SETTING = "/opt/setting/aileron-framework-web-cm.properties";
    
    /**
     * constractor
     * @throws ResourceNotFoundException 
     * @throws IOException 
     * @throws SQLException 
     * @throws ConvertorNotFoundException 
     * @throws ClassNotFoundException 
     * @throws ModuleConfigurationException 
     */
    public ApplicationModule() throws IOException, ResourceNotFoundException, ModuleConfigurationException, ClassNotFoundException, ConvertorNotFoundException, SQLException
    {
        globalSettingProperties(ResourceUtils.resource(GLOBAL_SETTING).toProperties());
        final boolean isDebug = globalSetting("system.debug").equals("true");

        this.auth = new AuthModule();
        this.dao = new DbDaoModule( new DaoConduct( new DbConnectionManagerSimple(globalSettingProperties()),isDebug ) );
        this.template = new XmlTemplateModule();
    }
    
    /**
     * auth
     */
    private final AuthModule auth;
    
    /**
     * dao
     */
    private final DbDaoModule dao;
    
    /**
     * template
     */
    private final XmlTemplateModule template;
    
    /*
     * (非 Javadoc)
     * @see com.google.inject.Module#configure(com.google.inject.Binder)
     */
    @Override
    public void configure(final Binder binder)
    {
        binder.install(auth);
        binder.install(dao);
        binder.install(template);
    }
}